package com.class5.springboot.frame.repo.tool.xml;

import com.class5.springboot.frame.repo.exception.RepositoryException;

import java.io.Serializable;
import java.sql.Date;
import java.sql.Timestamp;


@SuppressWarnings("serial")
public class DbTableColumn implements Serializable {
    public final static String INDEX_NORMAL = "normal";
    public final static String INDEX_UNIQUE = "unique";
    //列名
    private String name;
    //属性名
    private String field;
    //是否生成属性
    private boolean nofield;
    //数据类型
    private String columnType;
    //是否为主键
    private boolean identity = false;
    //是否需要使用数据库自增
    private boolean autoIncrement = false;
    //使用NormalRepository保存时，是否需要由程序生成唯一序列值
    private boolean autoIncByRepo = false;
    //默认值
    private String defaultValue;
    //是否充许为空
    private boolean nullable = true;
    //附加表达式
    private String extra;
    //备注
    private String comment;
    //详细说明
    private String notes;
    //关联的字典组
    private String refCodeGroup;
    //是否为版本字段
    private boolean version = false;
    //是否为存储文件ID列表
    private boolean fileField = false;


    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getComment() {
        return comment;
    }
    public void setComment(String c) {
        this.comment = c;
    }
    public boolean isNullable() {
        return nullable;
    }
    public void setNullable(boolean nullable) {
        this.nullable = nullable;
    }

    public boolean isIdentity() {
        return identity;
    }
    public void setIdentity(boolean identity) {
        this.identity = identity;
    }
    public boolean isVersion() {
        return version;
    }
    public void setVersion(boolean version) {
        this.version = version;
    }
    public String getField() {
        return field;
    }
    public void setField(String field) {
        this.field = field;
    }
    public String getLabel() {
        if(comment != null && comment.indexOf(":") > 0){
            return comment.substring(0, comment.indexOf(":"));
        }
        return comment;
    }
    /**
     * 获取对应的实体类属性类型
     * @return
     */
    public Class<?> getFieldType() {
        if(columnType.startsWith("varchar")
                || columnType.startsWith("text")) {
            return String.class;
        }
        if(columnType.startsWith("int") || columnType.startsWith("unsigned int")) {
            return Integer.class;
        }
        if(columnType.startsWith("bigint") || columnType.startsWith("unsigned bigint")) {
            return Long.class;
        }
        if(columnType.startsWith("double") || columnType.startsWith("float") ||
                columnType.startsWith("decimal")) {
            return Double.class;
        }
        if("date".equals(columnType)) {
            return Date.class;
        }
        if("datetime".equals(columnType) || "timestamp".equals(columnType)) {
            return Timestamp.class;
        }
        if("bit(1)".equals(columnType)) {
            return Boolean.class;
        }
        throw new RepositoryException("不支持数据类型" + columnType);
    }
    /**
     * 转换数据
     * @param value
     * @return
     */
    public Object convert(String value){
        if(value == null)
            return null;
        if(columnType.startsWith("varchar")
                || columnType.startsWith("text")) {
            return value;
        }
        if(columnType.startsWith("int") || columnType.startsWith("unsigned int")) {
            return value.trim().isEmpty()?null:Integer.valueOf(value.trim());
        }
        if(columnType.startsWith("bigint") || columnType.startsWith("unsigned bigint")) {
            return value.trim().isEmpty()?null:Long.valueOf(value.trim());
        }
        if(columnType.startsWith("double") || columnType.startsWith("float") ||
                columnType.startsWith("decimal")) {
            return value.trim().isEmpty()?null:Double.valueOf(value.trim());
        }
        if("date".equals(columnType)) {
            return value.trim().isEmpty()?null:java.sql.Date.valueOf(value.trim());
        }
        if("datetime".equals(columnType) || "timestamp".equals(columnType)) {
            return value.trim().isEmpty()?null:java.sql.Timestamp.valueOf(value.trim());
        }
        if("bit(1)".equals(columnType)) {
            return value.trim().isEmpty()?null:Boolean.valueOf(value.trim());
        }
        throw new RepositoryException("不支持数据类型" + columnType);
    }

    public String getNotes() {
        return notes;
    }
    public void setNotes(String notes) {
        this.notes = notes;
    }
    public String getRefCodeGroup() {
        return refCodeGroup;
    }
    public void setRefCodeGroup(String refCodeGroup) {
        this.refCodeGroup = refCodeGroup;
    }
    public boolean isNofield() {
        return nofield;
    }
    public void setNofield(boolean nofield) {
        this.nofield = nofield;
    }
    public String getColumnType() {
        return columnType;
    }
    public void setColumnType(String columnType) {
        this.columnType = columnType;
    }
    public String getDefaultValue() {
        return defaultValue;
    }
    public void setDefaultValue(String defaultValue) {
        this.defaultValue = defaultValue;
    }
    public String getExtra() {
        return extra;
    }
    public void setExtra(String extra) {
        this.extra = extra;
    }
    public boolean isFileField() {
        return fileField;
    }
    public void setFileField(boolean fileField) {
        this.fileField = fileField;
    }
    public boolean isAutoIncByRepo() {
        return autoIncByRepo;
    }
    public void setAutoIncByRepo(boolean autoIncByRepo) {
        this.autoIncByRepo = autoIncByRepo;
    }
    public boolean isAutoIncrement() {
        return autoIncrement;
    }
    public void setAutoIncrement(boolean autoIncrement) {
        this.autoIncrement = autoIncrement;
    }
    /**
     * 基本属性，是否一样，除了default value
     * @param old
     * @return
     */
    public boolean isAttrEqual(DbTableColumn old) {
        if(old == null)
            return false;
		/*System.out.println("-----attr equal-----");
		System.out.println("name:" + name + "," + old.name + "," + equal2(name, old.name));
		System.out.println("columnType:" + columnType + "," + old.columnType + "," + equal2(columnType, old.columnType));
		System.out.println("nullable:" + nullable + "," + old.nullable + "," + equal2(nullable, old.nullable));
		System.out.println("extra:" + extra + "," + old.extra + "," + equal2(extra, old.extra));*/

        return equal2(name, old.name)
                && equal2(columnType, old.columnType)
                && equal2(nullable, old.nullable)
                && equal2(extra, old.extra);
    }
    /**
     * default value是否一样
     * @param old
     * @return
     */
    public boolean isDefaultEqual(DbTableColumn old) {
        if(old == null)
            return false;
        String s1 = defaultValue;
        String s2 = old.defaultValue;
		/*System.out.println("--------default eqaul-----");
		System.out.println("name:" + name + "," + old.name + "," + equal2(name, old.name));
		System.out.println("defaultValue:" + s1 + "," + s2 + "," + equal2(s1, s2));
		*/

        if(s1 != null && s1.startsWith("'") && s1.endsWith("'")) {
            s1 = s1.substring(1, s1.length()-1);
        }
        if(s2 != null && s2.startsWith("'") && s2.endsWith("'")) {
            s2 = s2.substring(1, s2.length()-1);
        }

        return equal2(name, old.name)
                && equal2(s1, s2);
    }
    private boolean equal2(Object obj1, Object obj2) {
        if(obj1 == null) {
            obj1 = "";
        }
        if(obj2 == null) {
            obj2 = "";
        }
        if(obj1 != null && obj2 != null) {
            return obj1.toString().equalsIgnoreCase(obj2.toString());
        }else {
            return false;
        }
    }
    public boolean isNumberField() {
        if(columnType.startsWith("int") || columnType.startsWith("unsigned int")) {
            return true;
        }
        if(columnType.startsWith("bigint") || columnType.startsWith("unsigned bigint")) {
            return true;
        }
        if(columnType.startsWith("double") || columnType.startsWith("float") ||
                columnType.startsWith("decimal")) {
            return true;
        }
        if("bit(1)".equals(columnType)) {
            return true;
        }
        return false;
    }

}
